﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace LaoLuo.Common.Test
{


    /// <summary>
    ///这是 TextTemplateTest 的测试类，旨在
    ///包含所有 TextTemplateTest 单元测试
    ///</summary>
    [TestClass()]
    public class TextTemplateTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void RenderEntityTest()
        {
            Assert.AreEqual("test: username is test, password is 123, birthday is ", TextTemplate.Render("${username}: username is ${username}, password is ${password}, birthday is ${birthday}", new { Password = "123", UserName = "test", Age = 100 }, "${", "}", true));
            Assert.AreEqual("test: username is test, password is 123, birthday is ", TextTemplate.Render("${username}: username is ${username}, password is ${password}, birthday is ${birthday:yyyy}", new { Password = "123", UserName = "test", Age = 100 }, "${", "}"));
            Assert.AreEqual("test: username is test, password is 123, birthday is 2000", TextTemplate.Render("${username}: username is ${username}, password is ${password}, birthday is ${birthday:yyyy}", new { Password = "123", UserName = "test", Age = 100, Birthday = new DateTime(2000, 1, 1, 1, 1, 1) }, "${", "}"));
            Assert.AreEqual("test info: username is test, password is 123, birthday is ", TextTemplate.Render("${username} info: username is ${username}, password is ${password}, birthday is ${birthday}", new { Password = "123", UserName = "test", Age = 100 }, "${", "}"));

            Assert.AreEqual("test: username is test, password is , birthday is ", TextTemplate.Render("${UserName}: username is ${UserName}, password is ${password}, birthday is ${birthday}", new { Password = "123", UserName = "test", Age = 100 }, "${", "}", false));

            Assert.AreEqual("test: username is test, password is 123, birthday is ", TextTemplate.Render("${UserName}: username is ${username}, password is ${password}, birthday is ${birthday}", new { Password = "123", UserName = "test", Age = 100 }, "${", "}", true));
        }

        [TestMethod()]
        public void RenderDictionaryTest()
        {
            Assert.AreEqual("test: username is test, password is 123, birthday is ", TextTemplate.Render("${username}: username is ${username}, password is ${password}, birthday is ${birthday}", new Dictionary<string, object> { { "username", "test" }, { "Password", "123" } }, "${", "}"));

            Assert.AreEqual("test: username is test, password is , birthday is ", TextTemplate.Render("${username}: username is ${username}, password is ${password}, birthday is ${birthday}", new Dictionary<string, object> { { "username", "test" }, { "Password", "123" } }, "${", "}", false));

            Assert.AreEqual("test: username is test, password is 123, birthday is ", TextTemplate.Render("${username}: username is ${UserName}, password is ${password}, birthday is ${birthday}", new Dictionary<string, object> { { "username", "test" }, { "Password", "123" } }, "${", "}"));
        }

        [TestMethod()]
        public void RenderComplexTest()
        {
            // NotImplemented
            //Assert.AreEqual("Country : uk, School : bridge",
            //    TextTemplate.Render("Country : ${Country.Name}, School : ${School.Name}",
            //    new { Country = new Country { Name = "uk" }, School = new School { Name = "bridge" } },
            //    "${",
            //    "}")
            //    );
        }

        [TestMethod()]
        public void RenderBaseTest()
        {
            var n = new List<NoticeInfo>();

            n.Add(new NoticeWinInfo { TypeId = 1, Amount = 100 });
            n.Add(new NoticeWinInfo { TypeId = 1 });

            Assert.AreEqual("TypeId : 1, Amount : 100",
                    TextTemplate.Render("TypeId : ${TypeId}, Amount : ${Amount}",
                    n[0],
                    "${",
                    "}")
                    );

            Assert.AreEqual("TypeId : 1, Amount : 0",
                    TextTemplate.Render("TypeId : ${TypeId}, Amount : ${Amount}",
                    n[1],
                    "${",
                    "}")
                    );
        }

        class School
        {
            public string Name { get; set; }

            public IEnumerable<Teacher> Teachers { get; set; }
        }

        class Country
        {
            public string Name { get; set; }
        }

        class Teacher
        {
            public string Name { get; set; }
        }

        class NoticeInfo
        {
            public int TypeId { get; set; }
        }

        class NoticeWinInfo : NoticeInfo
        {
            public decimal Amount { get; set; }
        }

    }
}
